using System;
using System.Collections.Generic;
using System.Text;
using SqlTools.DataAccessLibrary;
using System.Data.SqlClient;
using System.Data;

namespace SqlTools.SampleDAL.Data
{
	public class DbRole : DbBase<RolePrimaryKey, Role>
	{
		public DbRole()
			: base("daltest")
		{
		}

		public DbRole(string connectionStringName)
			: base(connectionStringName)
		{

		}

		#region Select
		public override Role SelectByPK(RolePrimaryKey key)
		{
			Role role = null;
			using ( SqlConnection conn = new SqlConnection(_connectionString) )
			{
				SqlCommand cmd = new SqlCommand("daltest_Role_SelectByPK", conn);
				cmd.CommandType = CommandType.StoredProcedure;

				cmd.Parameters.Add("@RoleID", SqlDbType.Int).Value = key.RoleID;

				conn.Open();

				SqlDataReader sdr = cmd.ExecuteReader();

				if ( sdr.Read() )
				{
					role = Role.CreateFromReader(sdr);
					role._ConnectionStringName = _connectionStringName;
				}
				sdr.Close();

				conn.Close();
			}

			return role;
		}
		#endregion

		#region Insert
		public override RolePrimaryKey Insert(Role role, SqlConnection conn, SqlTransaction trans)
		{
			role._ConnectionStringName = _connectionStringName;

			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_Role_Insert", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@RoleID", SqlDbType.Int).Direction = ParameterDirection.Output;
				cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = role.Name;

				cmd.ExecuteNonQuery();

				if ( cmd.Parameters["@RoleID"].Value != DBNull.Value )
				{
					role.Key.RoleID = (int)cmd.Parameters["@RoleID"].Value;
				}

				role._UpdateState(ObjectState.None);
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}

			return role.Key;
		}
		#endregion

		#region Update
		public override int Update(Role role, SqlConnection conn, SqlTransaction trans)
		{
			role._ConnectionStringName = _connectionStringName;

			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_Role_Update", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@RoleID", SqlDbType.Int).Value = role.Key.RoleID;
				cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = role.Name;

				int rows = cmd.ExecuteNonQuery();

				role._UpdateState(ObjectState.None);

				return rows;
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}
		}
		#endregion

		#region Delete
		public override int Delete(Role role, SqlConnection conn, SqlTransaction trans)
		{
			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_Role_Update", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@RoleID", SqlDbType.Int).Value = role.Key.RoleID;

				int rows = cmd.ExecuteNonQuery();

				role._UpdateState(ObjectState.Deleted);

				return rows;
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}
		}
		#endregion
	}
}
